<script setup lang="ts">
import { getCurrentInstance, ref, reactive, onMounted } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const userInfo = proxy.$Store.get('userInfo') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
let globalData = {}
const form = reactive({
  remark: ''
})

/* ---- */
const layer = ref()
const layerLoading = ref()

/* ---- */
const getFormInfo = () => {
  if (proxy.$route.query.id) {
    proxy.$http.get('program/index', {
      params: { 'ProgramSearch[id]': proxy.$route.query.id }
    }).then((res) => {
      proxy.$Myfun.getJsonData(res.items[0].description, (resJson) => {
        globalData = resJson[0] || {}
        if (resJson[0].remark) form.remark = resJson[0].remark
      })
    }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
  }
}
// ----
const subform = async () => {
  globalData.remark = form.remark
  // ----
  const forDescCon = JSON.stringify(globalData)
  // ----
  if (proxy.$route.query.id) {
    const forType = 'patch'
    const forUrl = 'program/update?id=' + proxy.$route.query.id
    const forData = {
      description: forDescCon,
      // ----
      config_id: userInfo.config_id,
      openid: userInfo.openid
    }
    // ----
    proxy.$Myfun.onLoading(layerLoading.value, 'open', proxy.$Global.loadingMsg)
    let resData = ''
    await proxy.$http[forType](forUrl, forData).then((res) => { resData = res }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
    // ----
    proxy.$Myfun.disLoading(layerLoading.value, 'close')
    if (resData) proxy.$router.back()
  }
}
const goback = () => { proxy.$router.back() }

/* ---- */
onMounted(() => {
  getFormInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <div class="formSet">
      <ul>
        <li>
          <div class="form-are xw-radius-3">
            <p><textarea class="tarea-H4" placeholder="" v-model.trim="form.remark"></textarea></p>
          </div>
        </li>
      </ul>
      <div class="form-sub xw-radius-3 xw-bgCL-11 xw-act" @click="subform">确定提交</div>
      <div class="form-sub xw-radius-3 xw-WhiteRed xw-act" @click="goback">取消返回</div>
    </div>
    <layer-view ref="layer"/>
    <layer-view ref="layerLoading"/>
  </div>
</template>
